#!/bin/bash

export DEBUG_MODE=3

cd
mkdir -p /sdcard/Android/data/com.termux/files/Download
mkdir -p /sdcard/moboxtrace

. $PREFIX/glibc/opt/scripts/configs
load_configs

export LOG_PATH=/exbin/tmp/mobox
echo "  LOG_PATH: ${LOG_PATH}"
echo "WINEPREFIX: ${WINEPREFIX}"
set>$PREFIX/tmp/mobox_set.txt

echo ""
echo "正在启动 wine，请稍等"
touch $PREFIX/glibc/opt/virgl/virgl-enabled

function stop-all {
        # pkill -f "app_process / com.termux.x11"
        rm -rf $PREFIX/tmp/pulse-*
        pulseaudio -k &>/dev/null
        unset PULSE_SERVER
        pkill pulseaudio
        rm -rf $PREFIX/tmp/.virgl_test &>/dev/null
        pkill virgl
        rm -rf $PREFIX/tmp/.virgl_test &>/dev/null
}
stop-all
if [ -e $PREFIX/glibc/opt/virgl/virgl-enabled ]; then
        echo "正在启动virgl_server"
        export GALLIUM_DRIVER=virpipe
        export MESA_GL_VERSION_OVERRIDE=4.0

        chmod +x $PREFIX/glibc/opt/virgl/libvirgl_test_server.so
        TMPDIR=$PREFIX/tmp $PREFIX/glibc/opt/virgl/libvirgl_test_server.so &>/dev/null &

        # ln -sf ${CURRENT_OS_DIR}/tmp/.virgl_test $PREFIX/tmp/.virgl_test
fi
# pulseaudio --start --load="module-native-protocol-tcp auth-ip-acl=127.0.0.1 auth-anonymous=1" --exit-idle-time=-1
# termux-x11 :0 &>/dev/null &
sleep 1
if [ -e $PREFIX/glibc/opt/virgl/virgl-enabled ]; then
        chmod 777 $PREFIX/tmp/.virgl_test
fi
export RESOLUTION=1280x1024
if [ "$STARTUP_COMPATIBILITY_MODE" = "1" ]; then
        autores=""
else
        # autores=$(xrandr | grep current | awk '{print $8$9$10}' | tr -d ,)
        autores=""
fi
if [ ! "$autores" = "1280x1024" ] && [ ! "$autores" = "" ]; then
        export RESOLUTION=$autores
        echo "$autores">$PREFIX/glibc/opt/last-resolution.conf
elif [ -e $PREFIX/glibc/opt/last-resolution.conf ]; then
        export RESOLUTION=$(cat $PREFIX/glibc/opt/last-resolution.conf)
fi

chmod +x $PREFIX/glibc/bin/box64
chmod +x $WINE_PATH/bin/{wine,wineserver}
patchelf --force-rpath --set-rpath $PREFIX/glibc/lib --set-interpreter $PREFIX/glibc/lib/ld-linux-aarch64.so.1 $PREFIX/glibc/bin/box64
rm -rf $PREFIX/glibc/bin/{wine,wineserver}
ln -sf $WINE_PATH/bin/wine $PREFIX/glibc/bin/wine
ln -sf $WINE_PATH/bin/wineserver $PREFIX/glibc/bin/wineserver

export GLIBC_BIN=$PREFIX/glibc/bin
unset LD_PRELOAD

if [ ! -e $WINEPREFIX/.update-timestamp ]; then
        echo "Configuring $WINEPREFIX. This may take a few minutes"
        rm -rf $WINEPREFIX
        unset BOX64_DYNAREC_BIGBLOCK
        unset WINEESYNC
        unset WINEESYNC_TERMUX
        unset BOX64_DYNAREC_CALLRET
        if [ -e $WINE_PATH/lib/wine/i386-windows/shell32-bak.dll ] || [ -e $WINE_PATH/lib64/wine/x86_64-windows/shell32-bak.dll ]; then
                mv $WINE_PATH/lib/wine/i386-windows/shell32-bak.dll     $WINE_PATH/lib/wine/i386-windows/shell32.dll &>/dev/null
                mv $WINE_PATH/lib/wine/x86_64-windows/shell32-bak.dll   $WINE_PATH/lib/wine/x86_64-windows/shell32.dll &>/dev/null
                mv $WINE_PATH/lib64/wine/x86_64-windows/shell32-bak.dll $WINE_PATH/lib64/wine/x86_64-windows/shell32.dll &>/dev/null
                mv $WINE_PATH/lib64/wine/i386-windows/shell32-bak.dll   $WINE_PATH/lib64/wine/i386-windows/shell32.dll &>/dev/null
        fi

        WINEDLLOVERRIDES="winegstreamer=disabled,mscoree=disabled" $GLIBC_BIN/box64 $GLIBC_BIN/wine wineboot -u >${LOG_PATH}_wineboot.txt 2>&1
        ## $GLIBC_BIN/box64 $GLIBC_BIN/wine explorer /desktop=shell,$RESOLUTION winecfg
        if [ ! -e $WINEPREFIX/.update-timestamp ]; then
                echo "Cannot configure. See ${LOG_PATH}_wineboot.txt"
                $GLIBC_BIN/box64 $GLIBC_BIN/wineserver -k &>/dev/null
                export LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermux-exec.so
                rm -rf $PREFIX/tmp/pulse-*
                pulseaudio -k &>/dev/null
                pkill -f pulseaudio
                # pkill -f "app_process / com.termux.x11"
                sleep 5
                exit
        fi

        echo "disable">$WINEPREFIX/.update-timestamp
        7z x $PREFIX/glibc/opt/prefix/drive_c.7z -o$WINEPREFIX/drive_c -y &>/dev/null
        echo "Installing DirectX"
        7z x $PREFIX/glibc/opt/prefix/directx.7z -o$WINEPREFIX/drive_c -y &>/dev/null
        echo "Installing Start Menu shortcuts"
        cp -r $PREFIX/glibc/opt/prefix/start/* "$WINEPREFIX/drive_c/ProgramData/Microsoft/Windows/Start Menu"
        rm -rf "$WINEPREFIX/dosdevices/z:"
        ln -sf /data/data/com.termux/files "$WINEPREFIX/dosdevices/z:"
        ln -sf /sdcard/Download "$WINEPREFIX/dosdevices/d:"
        ln -sf /sdcard/Android/data/com.termux/files/Download "$WINEPREFIX/dosdevices/e:"
        echo "Installing registry tweaks"
        cp $PREFIX/glibc/opt/prefix/marlett.ttf $WINEPREFIX/drive_c/windows/Fonts
        $GLIBC_BIN/box64 $GLIBC_BIN/wine regedit $PREFIX/glibc/opt/prefix/user.reg &>/dev/null
        $GLIBC_BIN/box64 $GLIBC_BIN/wine regedit $PREFIX/glibc/opt/prefix/system.reg &>/dev/null
        mkdir -p "$WINEPREFIX/moboxmeta"
        $GLIBC_BIN/box64 $GLIBC_BIN/wine regedit $PREFIX/glibc/opt/prefix/fix-services.reg &>/dev/null
        touch $WINEPREFIX/moboxmeta/services-fix-applied
        tar -xf $PREFIX/glibc/opt/prefix/fix-fonts.tar.xz -C $WINEPREFIX/drive_c/windows
        touch $WINEPREFIX/moboxmeta/fonts-fix-applied
        touch $WINEPREFIX/moboxmeta/dxdlls-fix-applied

        if [ ! "$STARTUP_COMPATIBILITY_MODE" = "1" ]; then
                echo "Installing PhysX 9.10, 7-Zip, Visual C++ Redistributable"
                $GLIBC_BIN/box64 $GLIBC_BIN/wine explorer /desktop=shell,640x480 cmd /c "start Z:\\usr\\glibc\\opt\\apps\\install.bat" >${LOG_PATH}_redist.txt 2>&1
                
                echo "正在安装图形加速服务端"
                if [ ! -e $PREFIX/glibc/opt/virgl/virgl-enabled ]; then
                        sleep 1
                        echo "Installing dxvk-async-1.10.3"
						$GLIBC_BIN/box64 $GLIBC_BIN/wine explorer /desktop=shell,640x480 cmd /c "cd Z:\\usr\\glibc\\opt\\prefix\\d3d && start Z:\\usr\\glibc\\opt\\prefix\\d3d\\dxvk-async-1.10.3.bat" &>/dev/null
                        sleep 1
                        echo "Installing turnip-v5"
                        $GLIBC_BIN/box64 $GLIBC_BIN/wine explorer /desktop=shell,640x480 cmd /c "cd Z:\\usr\\glibc\\opt\\prefix\\mesa && start Z:\\usr\\glibc\\opt\\prefix\\mesa\\turnip-v5.bat" &>/dev/null
                else
                        sleep 1
                        echo "Installing wined3d-8.0.2"
                        $GLIBC_BIN/box64 $GLIBC_BIN/wine explorer /desktop=shell,640x480 cmd /c "cd Z:\\usr\\glibc\\opt\\prefix\\d3d && start Z:\\usr\\glibc\\opt\\prefix\\d3d\\wined3d-8.0.2.bat" &>/dev/null
                fi
        fi
        sleep 3

        if [ -e $WINE_PATH/lib/wine/i386-unix/libwine.so.1 ] || [ -e $WINE_PATH/lib64/wine/x86_64-unix/libwine.so.1 ]; then
                mv $WINE_PATH/lib/wine/i386-windows/shell32.dll $WINE_PATH/lib/wine/i386-windows/shell32-bak.dll &>/dev/null
                mv $WINE_PATH/lib/wine/x86_64-windows/shell32.dll $WINE_PATH/lib/wine/x86_64-windows/shell32-bak.dll &>/dev/null
                mv $WINE_PATH/lib64/wine/x86_64-windows/shell32.dll $WINE_PATH/lib64/wine/x86_64-windows/shell32-bak.dll &>/dev/null
                mv $WINE_PATH/lib64/wine/i386-windows/shell32.dll $WINE_PATH/lib64/wine/i386-windows/shell32-bak.dll &>/dev/null
        else
                rm -rf $WINE_PATH/lib/wine/i386-unix/shell32.dll.so &>/dev/null
                rm -rf $WINE_PATH/lib/wine/x86_64-unix/shell32.dll.so &>/dev/null
                rm -rf $WINE_PATH/lib64/wine/x86_64-unix/shell32.dll.so &>/dev/null
                rm -rf $WINE_PATH/lib64/wine/i386-unix/shell32.dll.so &>/dev/null
        fi
        $GLIBC_BIN/box64 $GLIBC_BIN/wineserver -k &>/dev/null
fi

if ! ls $PREFIX/glibc/opt/prefix/start/Registry/2.* &>/dev/null; then
        cp -r $PREFIX/glibc/opt/prefix/start/Registry/2.* $WINEPREFIX/drive_c/ProgramData/Microsoft/Windows/Start\ Menu/Registry
fi

rm -rf "$WINEPREFIX/drive_c/ProgramData/Microsoft/Windows/Start Menu/Install/media foundation (for RE)"

if [ ! -e "$WINEPREFIX/drive_c/ProgramData/Microsoft/Windows/Start Menu/Install/media foundation (for RE).lnk" ]; then
        cp "$PREFIX/glibc/opt/prefix/start/Install/media foundation (for RE).lnk" "$WINEPREFIX/drive_c/ProgramData/Microsoft/Windows/Start Menu/Install"
fi

rm -rf $PREFIX/glibc/opt/prefix/start-default

rm -d $PREFIX/glibc/opt/prefix/start/Install/1.* &>/dev/null
rm -d $PREFIX/glibc/opt/prefix/start/Install/2.* &>/dev/null
rm -d $WINEPREFIX/drive_c/ProgramData/Microsoft/Windows/Start\ Menu/Install/1.* &>/dev/null
rm -d $WINEPREFIX/drive_c/ProgramData/Microsoft/Windows/Start\ Menu/Install/2.* &>/dev/null

if ls $PREFIX/glibc/opt/prefix/start/Install/1.* &>/dev/null && ls $WINEPREFIX/drive_c/ProgramData/Microsoft/Windows/Start\ Menu/Install/1.* &>/dev/null; then
        rm -rf $WINEPREFIX/drive_c/ProgramData/Microsoft/Windows/Start\ Menu/Install/1.*/* &>/dev/null
        cp $PREFIX/glibc/opt/prefix/start/Install/1.*/* $WINEPREFIX/drive_c/ProgramData/Microsoft/Windows/Start\ Menu/Install/1.* &>/dev/null
fi

if ls $PREFIX/glibc/opt/prefix/start/Install/2.* &>/dev/null && ls $WINEPREFIX/drive_c/ProgramData/Microsoft/Windows/Start\ Menu/Install/2.* &>/dev/null; then
        rm -rf $WINEPREFIX/drive_c/ProgramData/Microsoft/Windows/Start\ Menu/Install/2.*/* &>/dev/null
        cp $PREFIX/glibc/opt/prefix/start/Install/2.*/* $WINEPREFIX/drive_c/ProgramData/Microsoft/Windows/Start\ Menu/Install/2.* &>/dev/null
fi

mkdir -p $WINEPREFIX/moboxmeta
if [ ! -f $WINEPREFIX/moboxmeta/services-fix-applied ]; then
        echo "Applying services fix"
        $GLIBC_BIN/box64 $GLIBC_BIN/wine regedit $PREFIX/glibc/opt/prefix/fix-services.reg &>/dev/null
        sleep 5
        $GLIBC_BIN/box64 $GLIBC_BIN/wineserver -k &>/dev/null
        touch $WINEPREFIX/moboxmeta/services-fix-applied
fi
if [ ! -f $WINEPREFIX/moboxmeta/fonts-fix-applied ]; then
        echo "Applying fonts fix"
        tar -xf $PREFIX/glibc/opt/prefix/fix-fonts.tar.xz -C $WINEPREFIX/drive_c/windows
        touch $WINEPREFIX/moboxmeta/fonts-fix-applied
fi
if [ ! -f $WINEPREFIX/moboxmeta/dxdlls-fix-applied ]; then
        echo "Applying dlls fix"
        7z x $PREFIX/glibc/opt/prefix/directx.7z -o$WINEPREFIX/drive_c -y &>/dev/null
        $GLIBC_BIN/box64 $GLIBC_BIN/wine regedit $PREFIX/glibc/opt/prefix/user.reg &>/dev/null
        sleep 5
        $GLIBC_BIN/box64 $GLIBC_BIN/wineserver -k &>/dev/null
        touch $WINEPREFIX/moboxmeta/dxdlls-fix-applied
fi

load_configs
export PULSE_SERVER=127.0.0.1
ln -sf $(df -H | grep -o "/storage/....-....") "$WINEPREFIX/dosdevices/f:" &>/dev/null
#export LC_ALL=$(cat $PREFIX/glibc/opt/locale.conf)
export LC_ALL=C.UTF-8

rm -rf "$WINEPREFIX/dosdevices/y:"
ln -sf /exbin "$WINEPREFIX/dosdevices/y:"
# ##export RESOLUTION=800x600
# #	cat <<- EOF >   /exbin/winedpi.reg
# #		REGEDIT4
# #		[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Hardware Profiles\Current\Software\Fonts]
# #		"LogPixels"=dword:00000096
# #	EOF
# #$GLIBC_BIN/box64 $GLIBC_BIN/wine regedit /exbin/winedpi.reg
# #echo "dpi设置结果：$?"
# #$GLIBC_BIN/box64 $GLIBC_BIN/wine explorer /desktop=shell,$RESOLUTION winecfg &
# # $GLIBC_BIN/box64 $GLIBC_BIN/wine $PREFIX/glibc/opt/apps/tfm.exe >$LOG_PATH 2>&1 &
# #$GLIBC_BIN/box64 $GLIBC_BIN/wine explorer /desktop=shell,$RESOLUTION start /min /unix $PREFIX/glibc/bin/ib.exe &>/dev/null &
# #$GLIBC_BIN/box64 $GLIBC_BIN/wine explorer /desktop=shell,$RESOLUTION $PREFIX/glibc/opt/apps/f5taskmgr.exe &>/dev/null &
# $GLIBC_BIN/box64 $GLIBC_BIN/wine explorer /desktop=shell,$RESOLUTION &>$LOG_PATH 2>&1 &
# $GLIBC_BIN/box64 $GLIBC_BIN/wine /desktop=shell,$RESOLUTION start cmd &>/dev/null &
# $GLIBC_BIN/box64 $GLIBC_BIN/wine cmd

$GLIBC_BIN/box64 $GLIBC_BIN/wine explorer /desktop=shell,$RESOLUTION $PREFIX/glibc/opt/apps/tfm.exe >$LOG_PATH 2>&1 &
sleep 2
$GLIBC_BIN/box64 $GLIBC_BIN/wine explorer /desktop=shell,$RESOLUTION $PREFIX/glibc/opt/apps/f5taskmgr.exe &>/dev/null &
sleep 2
$GLIBC_BIN/box64 $GLIBC_BIN/wine cmd

sleep 2
if [ "$STARTUP_WINEDEVICE_MODE" = "0" ]; then
        $GLIBC_BIN/box64 $GLIBC_BIN/wine taskkill /f /im services.exe &>/dev/null &
fi
# am start --user 0 -n com.termux.x11/.MainActivity &>/dev/null

# echo "要退出，请输入1并回车"
REBOOT=0
while true; do
        if [ -e $PREFIX/glibc/opt/shutdown ]; then
                rm -rf $PREFIX/glibc/opt/shutdown
                $GLIBC_BIN/box64 $GLIBC_BIN/wineserver -k &>/dev/null
                break
        fi
        if [ -e $PREFIX/glibc/opt/reboot ]; then
                rm -rf $PREFIX/glibc/opt/reboot
                $GLIBC_BIN/box64 $GLIBC_BIN/wineserver -k &>/dev/null
                REBOOT=1
                break
        fi
        # read -t 3 i
        # case "$i" in
        # 1)
        $GLIBC_BIN/box64 $GLIBC_BIN/wineserver -k &>/dev/null
        break
        # ;;
        # esac
done

export LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermux-exec.so
stop-all

if [ "$REBOOT" = "1" ]; then
        exec $0
fi
